package sql;

import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;


/**
 3.3 连接器属性
 属性	是否必填	默认值	类型 描述
 connector	required	(none)	String	‘datagen’
 rows-per-second	optional	10000 1000010000	Long	数据生产速率
 number-of-rows	optional	(none)	Long	指定生产的数据条数，默认是不限制
 fields.#.kind	optional	random	String	指定字段的生产数据的方式 random 还是 sequence
 fields.#.min	optional	(Minimum value of type)	(Type of field)	random 生成器的指定字段 # 最小值，支持数字类型
 fields.#.max	optional	(Maximum value of type)	(Type of field)	random 生成器的指定字段 # 最大值，支持数字类型
 fields.#.length	optional	100 100100	Integer	char / varchar / string / array / map / multiset 类型的长度
 fields.#.start	optional	(none)	(Type of field)	sequence 生成器的开始值
 fields.#.end	optional	(none)	(Type of field)	sequence 生成器的结束值
 */
public class D1Gen_div {

    public static void main(String[] args) {

        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);


        String genSql = "CREATE TABLE ods_tb ( " +
                " total BIGINT," +
                " used DOUBLE" +
                ") WITH ( " +
                " 'connector'='datagen', " +
                " 'rows-per-second'='1'," +
                " 'fields.used.kind'='random'," +
                " 'fields.used.min'='1000000'," +
                " 'fields.used.max'='1000000000'," +
                " 'fields.total.kind'='random'," +
                " 'fields.total.min'='1000000000'," +
                " 'fields.total.max'='1000000000000'" +
                ")";


        String sink = "CREATE TABLE pprint (" +
                "    total BIGINT, " +
                "    used DOUBLE, " +
                "    r1 DOUBLE ," +
                "    res STRING " +
                ") WITH (" +
                "    'connector' = 'print'" +
                ")";
        String sinkSql = "INSERT INTO pprint " +
                "SELECT " +
                "total, used, used/total r1," +
                " CAST(round(used/total, 4) AS STRING) as res" +
                "" +
                " " +
                " FROM ods_tb";

        tableEnv.executeSql(genSql);
        tableEnv.executeSql(sink);
        tableEnv.executeSql(sinkSql);



    }
}
